package org.argeproje.resim.proc.view;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;

import org.argeproje.resim.ResimProjectEditor;
import org.argeproje.resim.ViewerPage;
import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ExtremaDA;
import org.argeproje.resim.proc.data.HistogramDA;
import org.argeproje.resim.proc.data.ImageDA;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.sun.media.jai.widget.DisplayJAI;


public class ViewerPR extends Processor implements MouseListener, MouseMotionListener{
	private int _mousePressX, _mousePressY, _mouseDragX, _mouseDragY;
	protected ArrayList<JPanel> _viewPanelList = new ArrayList<JPanel>();
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(HistogramDA.class, 0);
		connRule.addInType(ImageDA.class, 0);
		connRule.addInType(ExtremaDA.class, 0);
	}
	public Data process() {
	   Processor pr = getProcessor();
	   pr.process();
	   return null;
    }
	protected ResimProjectEditor getProjectEditor()
	{
		return ResimProjectEditor.getActiveProjectEditor();	    
	}
	public JPanel getViewPanel(int index) {
		return _viewPanelList.get(index);
	}
	protected void setViewPanel(JPanel panel,String id,String title)
	{
		setViewPanel(panel,0,id,title);
	}
	protected void setViewPanel(JPanel panel,int panelIndex,String id,String title)
	{
		panel.addMouseMotionListener(this);
		panel.addMouseListener(this);
		_viewPanelList.add(panel);
		ResimProjectEditor rpe = getProjectEditor();
	    ViewerPage viewer = new ViewerPage(rpe,this,panelIndex,id,title);
		rpe.addPage(viewer,SWT.CLOSE);		
	}
	private Processor getProcessor() {
		Processor pr = null;
		Data input = getInput();
		if(input.isOfType(Data.IMAGE)){
			pr = new ImgViewerPR();		
		}else if(input.isOfType(Data.HISTOGRAM)){
			pr = new HistogramViewerPR();
		}else if(input.isOfType(Data.EXTREMA)){
			pr = new ExtremaViewerPR();
		}
		pr.setInput(input);
		return pr;
	}
	protected JPanel getShowingPanel(){
		ViewerPage vp = (ViewerPage) ResimProjectEditor.getActiveProjectEditor().getActivePageInstance();
		return _viewPanelList.get( vp.getPanelIndex() );
	}
	public void zoomIn(){}
	public void zoomOut(){}
	public void save(String fileName){}
	/*
	 * Translates view origin by x and y pixels
	 */
	protected void translateOrigin(int x, int y){
		
	}
	public void mouseDragged(MouseEvent e) {
      translateOrigin(e.getX() - _mousePressX, e.getY() - _mousePressY);
	  _mousePressX = e.getX();
	  _mousePressY = e.getY();
		
	}
	public void mouseMoved(MouseEvent e) {

		// TODO Auto-generated method stub
		
	}
	public void mouseClicked(MouseEvent e) {
	}
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseExited(MouseEvent e) {
		
	}
	public void mousePressed(MouseEvent e) {
		_mousePressX = e.getX();
		_mousePressY = e.getY();
		
	}
	public void mouseReleased(MouseEvent e) {

		
	}
}
